// KM_Elevator_ActionAdd = {
	// diag_log Format["(%1)[%2]: %3",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionAdd"];
	// If (X_CLIENT And local Player) then  {
		// _elevator_1 = ["LHD_Elevator_1"] call XNetGetJIP;
		// if ((_elevator_1 distance player) < 30) then {
			// _id = player getVariable "LHD_Elevator_1_Up";
			// If (_id < 0) then {
				// _id = player addAction ["Lower Elevator 1", "client\lhdelevator.sqf", [_elevator_1,0], 1, True, False, "", "(((getPosASL (['LHD_Elevator_1'] call XNetGetJIP)) select 2) > -10)"];
				// player setVariable ["LHD_Elevator_1_Up",_id];
			// };
			// _id = player getVariable "LHD_Elevator_1_Down";
			// If (_id < 0) then {
				// _id = player addAction ["Raise Elevator 1", "client\lhdelevator.sqf", [_elevator_1,1], 1, True, False, "", "(((getPosASL (['LHD_Elevator_1'] call XNetGetJIP)) select 2) < -3)"];
				// player setVariable ["LHD_Elevator_1_Down",_id];
			// };
			// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionAdd", "Added Elevator 1 Actions"];
		// };
		// _elevator_2 = ["LHD_Elevator_2"] call XNetGetJIP;
		// if ((_elevator_2 distance player) < 30) then {
			// _id = player getVariable "LHD_Elevator_2_Up";
			// If (_id < 0) then {
				// _id = player addAction ["Lower Elevator 2", "client\lhdelevator.sqf", [_elevator_2,0], 1, True, False, "", "(((getPosASL (['LHD_Elevator_2'] call XNetGetJIP)) select 2) > -10)"];
				// player setVariable ["LHD_Elevator_2_Up",_id];
			// };
			// _id = player getVariable "LHD_Elevator_2_Down";
			// If (_id < 0) then {
				// _id = player addAction ["Raise Elevator 2", "client\lhdelevator.sqf", [_elevator_2,1], 1, True, False, "", "(((getPosASL (['LHD_Elevator_2'] call XNetGetJIP)) select 2) < -3)"];
				// player setVariable ["LHD_Elevator_2_Down",_id];
			// };
			// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionAdd", "Added Elevator 1 Actions"];
		// };
	// };
// };

// KM_Elevator_ActionRemove = {
	// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionRemove", "Removing Actions"];
	// If (X_CLIENT And local Player) then  {
		// _id = player getVariable "LHD_Elevator_1_Up";
		// if (_id > 0) then {
			// player RemoveAction _id;
			// player setVariable ["LHD_Elevator_1_Up",-9999];
			// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionRemove", "Removed Elevator 1 Action 1"];
		// };
		// _id = player getVariable "LHD_Elevator_1_Down";
		// if (_id > 0) then {
			// player RemoveAction _id;
			// player setVariable ["LHD_Elevator_1_Down",-9999];
			// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionRemove", "Removed Elevator 1 Action 2"];
		// };
		
		// _id = player getVariable "LHD_Elevator_2_Up";
		// if (_id > 0) then {
			// player RemoveAction _id;
			// player setVariable ["LHD_Elevator_2_Up",-9999];
			// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionRemove", "Removed Elevator 2 Action 1"];
		// };
		// _id = player getVariable "LHD_Elevator_2_Down";
		// if (_id > 0) then {
			// player RemoveAction _id;
			// player setVariable ["LHD_Elevator_2_Down",-9999];
			// diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"createcarrier.sqf","KM_Elevator_ActionRemove", "Removed Elevator 2 Action 2"];
		// };
	// };
// };


If (isServer and (!isNil "LHD_Center")) then {
	if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3",diag_tickTime,"createcarrier.sqf",Format["Creating the LHD at %1 / %2 / %3", getPos LHD_Center, getPosASL LHD_Center, getPosATL LHD_Center]]; };
	_height = 0 - ((getPos LHD_Center) select 2);
	_LHD_5 = objNull;
	_LHDspawnpoint = [getPosASL LHD_Center select 0, getPosASL LHD_Center select 1, -0.9];
	{
		// This gets the bits of the ship closer to each others to avoid falling between blocks.
		_dummy = createVehicle [_x, _LHDspawnpoint, [], 0, "NONE"];
		_dummy setdir getDir LHD_Center;
		_dummy setPos _LHDspawnpoint;
		_dummy allowDamage false;
		if (_x == "Land_LHD_5") then {_LHD_5 = _dummy;};
	} foreach ["Land_LHD_1","Land_LHD_2","Land_LHD_3","Land_LHD_4","Land_LHD_5","Land_LHD_6"];
	// Here we use the same LHD house that we turn around to collate them.
	_LHDspawnpoint = LHD_Center modelToWorld [2,0,0];
	_LHDspawnpoint = [_LHDspawnpoint select 0, _LHDspawnpoint select 1, -0.9];
	_dummy = createVehicle ["Land_LHD_house_1", _LHDspawnpoint, [], 0, "NONE"];
	_dummy setdir getDir LHD_Center;
	_dummy setPos _LHDspawnpoint;
	_dummy allowDamage false;

	_LHDspawnpoint = LHD_Center modelToWorld [15.12,-13.2,0];
	_LHDspawnpoint = [_LHDspawnpoint select 0, _LHDspawnpoint select 1, -0.817];
	_dummy = createVehicle ["Land_LHD_house_1", _LHDspawnpoint, [], 0, "NONE"];
	_dummy setdir ((getDir LHD_Center) + 180);
	_dummy setPos _LHDspawnpoint;
	_dummy allowDamage false;

	// Now we add an open container which will serve as a teleport area.
	_LHDspawnpoint = _LHD_5 modelToWorld [12.5,26,0];
	_LHDspawnpoint = [_LHDspawnpoint select 0, _LHDspawnpoint select 1, (((getPosASL _LHD_5) select 2) + 15.5)];
	_LHD_Teleporter_Landing = createVehicle ["FlagCarrierUSA", _LHDspawnpoint, [], 0, "NONE"];
	_LHD_Teleporter_Landing setdir getDir LHD_Center;
	_LHD_Teleporter_Landing setPosASL _LHDspawnpoint;
	If (d_own_side == "WEST") then {
		_LHD_Teleporter_Landing setflagtexture "\ca\data\flag_usa_co.paa";
	} else {
		_LHD_Teleporter_Landing setflagtexture "\ca\data\flag_rus_co.paa";
	};

	_LHDspawnpoint = _LHD_5 modelToWorld [6,5,0];;
	_LHDspawnpoint = [_LHDspawnpoint select 0, _LHDspawnpoint select 1, (((getPosASL _LHD_Teleporter_Landing) select 2) + 0.40)];
	_LHD_mash = createVehicle ["MASH", _LHDspawnpoint, [], 0, "NONE"];
	_LHD_mash setDir (getDir LHD_Center + 90);
	_LHD_mash setPosASL _LHDspawnpoint;
	_LHD_mash allowDamage false;
	
	_LHDspawnpoint = getPosASL LHD_Center;
	// _LHDspawnpoint = LHD_Center modelToWorld [2,0,0];
	_LHDspawnpoint = [_LHDspawnpoint select 0, _LHDspawnpoint select 1, -0.9];
	_elevator_1 = createVehicle ["Land_LHD_elev_R", _LHDspawnpoint, [], 0, "NONE"];
	_elevator_1 setDir getDir LHD_Center;
	_elevator_1 setPos _LHDspawnpoint;
	_elevator_1 allowDamage false;
	_elevator_1 setVariable ["StartHeight",(getPosATL _elevator_1) select 2,True];
	_pos = _elevator_1 modelToWorld [19,0,18];
	// _trigger = [_pos, [10,10,0,False], ["ANY", "PRESENT", true], ["this", "call KM_Elevator_ActionAdd;", "call KM_Elevator_ActionRemove;"]] call XfCreateTrigger;
	_bucket = createVehicle ["MetalBucket", _pos, [], 0, "NONE"];
	_bucket setPos _pos;
	["LHD_Elevator_1",_elevator_1] call XNetSetJIP;
	["LHD_Elevator_1_Pos",_pos] call XNetSetJIP;
	
	_LHDspawnpoint = getPosASL LHD_Center;
	_LHDspawnpoint = LHD_Center modelToWorld [0,-134.6,0];
	_LHDspawnpoint = [_LHDspawnpoint select 0, _LHDspawnpoint select 1, -0.6];
	_elevator_2 = createVehicle ["Land_LHD_elev_R", _LHDspawnpoint, [], 0, "NONE"];
	_elevator_2 setDir (getDir LHD_Center + 180);
	_elevator_2 setPos _LHDspawnpoint;
	_elevator_2 allowDamage false;
	_elevator_2 setVariable ["StartHeight",(getPosATL _elevator_2) select 2,True];
	_pos = _elevator_2 modelToWorld [19,0,18];
	// _trigger = [_pos, [10,10,0,False], ["ANY", "PRESENT", true], ["this", "call KM_Elevator_ActionAdd;", "call KM_Elevator_ActionRemove;"]] call XfCreateTrigger;
	_bucket = createVehicle ["MetalBucket", _pos, [], 0, "NONE"];
	_bucket setPos _pos;
	["LHD_Elevator_2",_elevator_2] call XNetSetJIP;
	["LHD_Elevator_2_Pos",_pos] call XNetSetJIP;
	
	
	["LHD_Teleporter_Landing",_LHD_Teleporter_Landing] call XNetSetJIP;
};

waituntil {!isNil "X_INIT"};
If (X_Client) then {
	waituntil {!isNil "g_bCommonFuncsCompiled"};
	waituntil {!isNil "XNetGetJIP"};
	if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3",diag_tickTime,"createcarrier.sqf","Starting player init"]; };
	player setVariable ["LHD_Elevator_1_Up",-9999];
	player setVariable ["LHD_Elevator_1_Down",-9999];
	player setVariable ["LHD_Elevator_2_Up",-9999];
	player setVariable ["LHD_Elevator_2_Down",-9999];

	if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3",diag_tickTime,"createcarrier.sqf",Format["start to wait LHD: %1",(["LHD_Teleporter_Landing",False] call XNetGetJIP)]]; };
	waituntil {typename (["LHD_Teleporter_Landing",False] call XNetGetJIP) != "BOOLEAN"};
	_LHD_Teleporter_Landing = ["LHD_Teleporter_Landing",objNull] call XNetGetJIP;
	_LHD_Teleporter_Landing setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
	
	if (!isnull _LHD_Teleporter_Landing) then {
	_boxownpos = _LHD_Teleporter_Landing modelToWorld [0,-6,0];
	_boxownpos = [_boxownpos select 0, _boxownpos select 1, (((getPosASL _LHD_Teleporter_Landing) select 2) + 0.40)];
	_boxenemypos = _LHD_Teleporter_Landing modelToWorld [0,-9,0];
	_boxenemypos = [_boxenemypos select 0, _boxenemypos select 1, (((getPosASL _LHD_Teleporter_Landing) select 2) + 0.4)];
	
	[_boxownpos,_boxenemypos] spawn {
			waituntil {!(isnil "d_the_base_box") && !(isnil "x_weaponcargo")};
			_boxc_ownside = d_the_base_box createVehicleLocal (_this select 0);
			_boxc_ownside setPosASL (_this select 0);
			_boxc_ownside setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
			player reveal _boxc_ownside;
			_boxc_ownside allowdamage False;
			if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"createcarrier.sqf","Own Ammo Box created at", getPos _boxc_ownside]; };
			_boxc_otherside = objNull;
			If (d_create_both_boxes) then {
				_boxc_otherside = d_the_base_box createVehicleLocal (_this select 1);
				_boxc_otherside setPosASL (_this select 1);
				_boxc_otherside setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
				player reveal _boxc_otherside;
				_boxc_otherside allowdamage False;
				if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"createcarrier.sqf","Other Ammo Box created at", getPos _boxc_otherside]; };
			};
			_other_side = "EAST";
			If (d_own_side == "WEST") then {_other_side = "EAST";} else {_other_side = "WEST";};
			// This will wait for the server script to create the weapon arrays. From there it will start to fill the boxes.
			while {count((["d_weapon_array_enemy",[]] call XNetGetJIP)) <= 0} do {
				sleep 0.5;
			};

			while {True} do{
				if (!isnull _boxc_otherside) then {
					if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"createcarrier.sqf","Filling the enemy box", getPos _boxc_otherside]; };
					[_boxc_otherside,_other_side] spawn x_weaponcargo;
				};
				if (!isnull _boxc_ownside) then {
					if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"createcarrier.sqf","Filling the side box", getPos _boxc_ownside]; };
					[_boxc_ownside,d_own_side] spawn x_weaponcargo;
				};

				if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"createcarrier.sqf","Starting to wait", getPos _boxc_ownside]; };
				// We wait that the player gets close to the box before starting the refill wait. This avoids unnecessary refill when the player is in the field.
				waituntil { sleep 1; ((player distance _boxc_ownside) < 100) };
				if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"createcarrier.sqf","Waiting for player to go away", getPos _boxc_ownside]; };
				sleep 1200;
				// We wait that the player is far enough from the box before filling it.
				waituntil { sleep 5; ((player distance _boxc_ownside) > 100) };
			};
		};
	};
};